<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" border stripe :apiObj="apiObj" row-key="id" @selection-change="selectionChange"
				hidePagination>
				<el-table-column align="center" label="组织名称" prop="label"></el-table-column>
				<el-table-column align="center" label="是否启用" prop="enable">
					<template #default="scope">
						<el-switch v-model="scope.row.enable" @change="changeSwitch($event, scope.row)"
							:loading="scope.row.$switch_status" :active-value="0" :inactive-value="1"></el-switch>
					</template>
				</el-table-column>
				<el-table-column align="center" label="组织编码" prop="num"></el-table-column>
				<!-- <el-table-column align="center" label="部门名称" prop="depName"></el-table-column> -->

				<!-- <el-table-column label="备注" prop="remark" min-width="300"></el-table-column> -->
				<!-- <el-table-column  label="操作" fixed="right" align="center"> -->
				<el-table-column label="操作" align="center">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small"
								@click="depMapping(scope.row, scope.$index)">部门映射</el-button>

							<el-button text type="primary" size="small"
								@click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small"
								@click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save = false">
	</save-dialog>
	<edit-dialog v-if="dialog.edit" ref="editDialog" @success="handleSaveSuccess" @closed="dialog.edit = false">
	</edit-dialog>

	<dep-mapping-dialog v-if="dialog.depMapping" ref="depMappingDialog" @success="handleSaveSuccess"
		@closed="dialog.depMapping = false">
	</dep-mapping-dialog>
</template>

<script>
import saveDialog from './add'
import editDialog from './edit'
import depMappingDialog from './depMapping'
export default {
	name: 'org',
	components: {
		saveDialog,
		editDialog,
		depMappingDialog
	},
	data() {
		return {
			index: false,
			dialog: {
				save: false,
				edit: false,
				depMapping: false
			},
			apiObj: this.$API.basic.org.list,
			selection: [],
			search: {
				keyword: null
			}
		}
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.edit = true
			this.$nextTick(() => {
				this.$refs.editDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.edit = true
			this.$nextTick(() => {
				this.$refs.editDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row) {
			var reqData = { orgId: row.id, depId: row.depId }
			var res = await this.$API.basic.org.delete.post({ data: reqData });
			if (res.code == 0) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//表格内开关
		async changeSwitch(val, row) {

			row.enable = row.enable == 1 ? 0 : 1
			row.$switch_status = true;
			let data = {
				enable: val,
				id: row.id
			}
			var res = await this.$API.basic.org.enable.post({ data: data });
			if (res && res.code == 0) {
				setTimeout(() => {
					delete row.$switch_status;
					row.enable = val;
					// console.log(typeof val, row)
					this.$message.success("操作成功")
				}, 500)
			}

		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {

		},
		//根据ID获取树结构
		filterTree(id) {
			var target = null;
			function filter(tree) {
				tree.forEach(item => {
					if (item.id == id) {
						target = item
					}
					if (item.children) {
						filter(item.children)
					}
				})
			}
			filter(this.$refs.table.tableData)
			return target
		},

		//本地更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = false
			this.dialog.depMapping = false
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'depMapping') {
				this.$refs.table.refresh()
			}
		},
		//部门映射
		depMapping(row, index) {
			this.dialog.depMapping = true
			this.$nextTick(() => {
				this.$refs.depMappingDialog.open().setData(row)
			})

		}
	}
}
</script>

<style></style>
